<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Lyrics</title>
	<script type="text/javascript" src="../js/lyrics.js"></script>
	<link rel="stylesheet" href="../css/lyricer.css">
	<link href="https://fonts.googleapis.com/css?family=M+PLUS+1p" rel="stylesheet">
</head>
<body style="margin: 0;">
    <img style="" id="backgroundImage" alt="Background Image"></img>
	<div style="width: 100%; height: 100%; opacity: 0.8;  z-index: 1; background-color: rgba(0, 0, 0, 0.5); position: absolute;"></div>
	<div id="lyricer">
	</div>
	<script type="text/javascript">
	const { ipcRenderer } = require('electron');

	function changeStylesheetRule(stylesheet, selector, property, value) {
	selector = selector.toLowerCase();
	property = property.toLowerCase();
	value = value.toLowerCase();

	for(var i = 0; i < stylesheet.cssRules.length; i++) {
		var rule = stylesheet.cssRules[i];
		if(rule.selectorText === selector) {
			rule.style[property] = value;
			return;
		}
	}
  
	stylesheet.insertRule(selector + " { " + property + ": " + value + "; }", 0);
    }

	var text = "";
	var lrc = new Lyricer();
	ipcRenderer.on('neteasecors', function (event, lrcfile) {
		if (lrcfile.startsWith("netease=")){	
		try{	
		var url  = "https://music.163.com/api/search/pc?s="+lrcfile.substring(8)+"&type=1&limit=1";
        var req = new XMLHttpRequest();  
        req.overrideMimeType("application/json");
        req.open('GET', url, true);
        req.onload  = function() {
		try{	
        var jsonResponse = JSON.parse(req.responseText);                
        var id = jsonResponse["result"]["songs"][0]["id"];
        var url2  = "https://music.163.com/api/song/lyric?os=pc&id="+id+"&lv=-1&kv=-1&tv=-1";
        var req2 = new XMLHttpRequest();  
        req2.overrideMimeType("application/json");
        req2.open('GET', url2, true);
        req2.onload  = function() {
		try{
			var jsonResponse2 = JSON.parse(req2.responseText);
			var lyrics = jsonResponse2["lrc"]["lyric"];
			ipcRenderer.send('LyricsHandlerNE',lyrics);	
        window.close();} 
		catch (e) {
			ipcRenderer.send('LyricsHandlerNE','[00:00] Instrumental. / Lyrics not found.');
            window.close();	
		}
	    };
        req2.send();
		}catch(e){
			ipcRenderer.send('LyricsHandlerNE','[00:00] Instrumental. / Lyrics not found.');
            window.close();	
		}
	    };
        req.send();
	}catch(e){
		console.log(e);
		ipcRenderer.send('LyricsHandlerNE','[00:00] Instrumental. / Lyrics not found.');
        window.close();		
		} } 
	else{}
    });
	

	lrc.setLrc(text);
	</script>
</body>
</html>